using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;


namespace Microsoft.ConnectionEngine.Registers.Health.Patient.DataAccess
{
    public class DataAccessHelper
    {
        public static SqlConnection GetConnection()
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[GetDefaultConnectionStringName()].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            return connection;
        }

        public static string GetDefaultConnectionStringName()
        {
            return DataAccessResources.ConnectionName;
        }

        public static void CloseConnection(SqlConnection connection)
        {
            if (connection != null && connection.State != System.Data.ConnectionState.Closed)
                connection.Close();
        }

        public static object IfNull(object obj)
        {
            if (obj == null)
                return DBNull.Value;
            
            if (obj.GetType() == typeof(DateTime))
            {
                if ((DateTime)obj < DateTime.Parse("1 Jan 1750"))
                    return DBNull.Value;
                else
                    return obj;
            }
            
            return obj;

        }
    }
}
